﻿using System;
using System.Collections.Generic;
using System.Linq;
using Pap.Data.Abstraction.Utils;

namespace Pap.Data.LToS.Repositories
{
    /// <summary>
    /// 
    /// </summary>
    public class LoanRepositoryImpl : ILoanRepository
    {
        /// <summary>
        /// 
        /// </summary>
        private IDatabase _Database;

        /// <summary>
        /// Initializes a new instance of the <see cref="LoanRepositoryImpl"/> class.
        /// </summary>
        /// <param name="database">The database.</param>
        public LoanRepositoryImpl(IDatabase database)
        {
            this._Database = database;
        }

        #region Customers


        /// <summary>
        /// Gets the customer by ID.
        /// </summary>
        /// <param name="CustomerID">The customer ID.</param>
        /// <returns></returns>
        public ICustomer GetCustomerByID(Guid CustomerID)
        {
            return _Database.Customers.SingleOrDefault(c => c.CustomerID == CustomerID);
        }

        /// <summary>
        /// Finds the customers.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ICustomer> FindCustomers(System.Linq.Expressions.Expression<Func<ICustomer, bool>> Where)
        {
            return _Database.Customers.Where(Where);
        }

        /// <summary>
        /// Creates the customer.
        /// </summary>
        /// <param name="customer">The customer.</param>
        public void CreateCustomer(ICustomer customer)
        {
            _Database.Insert<ICustomer>(customer);
        }

        /// <summary>
        /// Deletes the customer.
        /// </summary>
        /// <param name="customer">The customer.</param>
        public void DeleteCustomer(ICustomer customer)
        {
            _Database.DeleteAll<IPhone>(customer.Phones);
            _Database.Delete<ICustomer>(customer);
        }

        /// <summary>
        /// Deletes the customers.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteCustomers(System.Linq.Expressions.Expression<Func<ICustomer, bool>> Where)
        {
            _Database.DeleteAll<ICustomer>(_Database.Customers.Where(Where));
        }

        #endregion

        #region EducationalLevels

        /// <summary>
        /// Gets the educational level by ID.
        /// </summary>
        /// <param name="EducationalLevelID">The educational level ID.</param>
        /// <returns></returns>
        public ILookUpEntity GetEducationalLevelByID(Guid EducationalLevelID)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Finds the educational levels.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> FindEducationalLevels(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            return this._Database.EducationalLevels.OfType<ILookUpEntity>();
        }

        /// <summary>
        /// Creates the educational level.
        /// </summary>
        /// <param name="City">The city.</param>
        public void CreateEducationalLevel(ILookUpEntity City)
        {
            //ICustomer customer = _Database.Customers.OfType<ICustomer>().SingleOrDefault(c => c.CustomerID == CustomerID);
            //return customer;
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the educational levels.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteEducationalLevels(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            throw new NotImplementedException();
            //ICustomer customer = _Database.Customers.OfType<ICustomer>().SingleOrDefault(c => c.SSID == ssid);
            //return customer;
        }

        #endregion

        #region SocialStatuses

        /// <summary>
        /// Gets the social status by ID.
        /// </summary>
        /// <param name="SocialStatusID">The social status ID.</param>
        /// <returns></returns>
        public ILookUpEntity GetSocialStatusByID(Guid SocialStatusID)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Finds the social statuses.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> FindSocialStatuses(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            return this._Database.SocialStatuses.OfType<ILookUpEntity>();
        }

        /// <summary>
        /// Creates the social status.
        /// </summary>
        /// <param name="SocialStatus">The social status.</param>
        public void CreateSocialStatus(ILookUpEntity SocialStatus)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the social statuss.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteSocialStatuss(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets all customer loans.
        /// </summary>
        /// <param name="customerID">The customer ID.</param>
        /// <returns></returns>
        public IEnumerable<ICustomerLoan> GetAllCustomerLoans(Guid customerID)
        {
            return _Database.CustomerLoans.Where(l => l.CustomerID == customerID);
        }

        /// <summary>
        /// Gets the name of the project.
        /// </summary>
        /// <param name="projectID">The project ID.</param>
        /// <returns></returns>
        public string GetProjectName(Guid projectID)
        {
            string name = _Database.Projects.SingleOrDefault(p => p.ProjectID == projectID).ProjectName;
            return name;
        }

        /// <summary>
        /// Gets the name of the project stage.
        /// </summary>
        /// <param name="projectStageID">The project stage ID.</param>
        /// <returns></returns>
        public int GetProjectStageName(Guid projectStageID)
        {
            int stage = _Database.ProjectStages.SingleOrDefault(p => p.ProjectStageID == projectStageID).Stage;
            return stage;
        }

        #endregion

        #region CustomerLoan

        /// <summary>
        /// Gets the customer loan by ID.
        /// </summary>
        /// <param name="CustomerLoanID">The customer loan ID.</param>
        /// <returns></returns>
        public ICustomerLoan GetCustomerLoanByID(Guid CustomerLoanID)
        {
            return this._Database.CustomerLoans.Single(loan => loan.LoanID == CustomerLoanID);
        }

        /// <summary>
        /// Finds the customer loans.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ICustomerLoan> FindCustomerLoans(System.Linq.Expressions.Expression<Func<ICustomerLoan, bool>> Where)
        {
            return this._Database.CustomerLoans.Where(Where);
        }

        /// <summary>
        /// Creates the customer loan.
        /// </summary>
        /// <param name="CustomerLoan">The customer loan.</param>
        public void CreateCustomerLoan(ICustomerLoan CustomerLoan)
        {
            this._Database.Insert<ICustomerLoan>(CustomerLoan);
        }

        /// <summary>
        /// Deletes the customer loan.
        /// </summary>
        /// <param name="customerLoan">The customer loan.</param>
        public void DeleteCustomerLoan(ICustomerLoan customerLoan)
        {
            this._Database.Delete<ICustomerLoan>(customerLoan);
        }

        /// <summary>
        /// Deletes the customer loans.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteCustomerLoans(System.Linq.Expressions.Expression<Func<ICustomerLoan, bool>> Where)
        {
            this._Database.DeleteAll<ICustomerLoan>(_Database.CustomerLoans.Where(Where));
        }

        /// <summary>
        /// Gets the last loan number.
        /// </summary>
        /// <returns></returns>
        public int GetLastLoanNumber()
        {
            IQueryable<ICustomerLoan> loans = _Database.CustomerLoans;
            if (loans.Count() > 0)
            {
                int max = loans.Max(s => s.LoanNumber);
                return max;
            }
            else return 0;
        }

        #endregion

        #region LoanLocations


        /// <summary>
        /// Gets the loan place by ID.
        /// </summary>
        /// <param name="LoanPlaceID">The loan place ID.</param>
        /// <returns></returns>
        public ILookUpEntity GetLoanPlaceByID(Guid LoanPlaceID)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Finds the loan places.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> FindLoanPlaces(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            return this._Database.LoanPlaceTypes.Where(Where);
        }

        /// <summary>
        /// Creates the loan place.
        /// </summary>
        /// <param name="LoanPlace">The loan place.</param>
        public void CreateLoanPlace(ILookUpEntity LoanPlace)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the loan places.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteLoanPlaces(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region LegalForms

        /// <summary>
        /// Gets the legal form by ID.
        /// </summary>
        /// <param name="LegalFormID">The legal form ID.</param>
        /// <returns></returns>
        public ILookUpEntity GetLegalFormByID(Guid LegalFormID)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Finds the legal forms.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> FindLegalForms(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            return this._Database.LoanLegalForms.Where(Where);
        }

        /// <summary>
        /// Creates the legal form.
        /// </summary>
        /// <param name="LegalForm">The legal form.</param>
        public void CreateLegalForm(ILookUpEntity LegalForm)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the legal forms.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteLegalForms(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region LoanTypes


        /// <summary>
        /// Gets the loan type by ID.
        /// </summary>
        /// <param name="LoanTypeID">The loan type ID.</param>
        /// <returns></returns>
        public ILookUpEntity GetLoanTypeByID(Guid LoanTypeID)
        {
            return this._Database.LoanTypes.Single(t => t.ID == LoanTypeID);
        }

        /// <summary>
        /// Finds the loan types.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> FindLoanTypes(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            return this._Database.LoanTypes.Where(Where);
        }

        /// <summary>
        /// Creates the type of the loan.
        /// </summary>
        /// <param name="LoanType">Type of the loan.</param>
        public void CreateLoanType(ILookUpEntity LoanType)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the loan types.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteLoanTypes(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region LoanPaymentMethods


        /// <summary>
        /// Gets the loan payment method by ID.
        /// </summary>
        /// <param name="LoanPaymentMethodID">The loan payment method ID.</param>
        /// <returns></returns>
        public ILoanPaymentMethod GetLoanPaymentMethodByID(Guid LoanPaymentMethodID)
        {
            return this._Database.LoanPaymentMethod.Single(m => m.LoanPaymentMethodID == LoanPaymentMethodID);
        }

        /// <summary>
        /// Finds the loan payment methods.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanPaymentMethod> FindLoanPaymentMethods(System.Linq.Expressions.Expression<Func<ILoanPaymentMethod, bool>> Where)
        {
            return this._Database.LoanPaymentMethod.Where(Where);
        }

        /// <summary>
        /// Creates the loan payment method.
        /// </summary>
        /// <param name="LoanPaymentMethod">The loan payment method.</param>
        public void CreateLoanPaymentMethod(ILoanPaymentMethod LoanPaymentMethod)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the loan payment methods.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteLoanPaymentMethods(System.Linq.Expressions.Expression<Func<ILoanPaymentMethod, bool>> Where)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region MilitaryStatus


        /// <summary>
        /// Gets the military status by ID.
        /// </summary>
        /// <param name="MilitaryStatusID">The military status ID.</param>
        /// <returns></returns>
        public ILookUpEntity GetMilitaryStatusByID(Guid MilitaryStatusID)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Finds the military status.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> FindMilitaryStatus(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            return this._Database.MilitaryStatuses.Where(Where);
        }

        /// <summary>
        /// Creates the military status.
        /// </summary>
        /// <param name="LoanPaymentMethod">The loan payment method.</param>
        public void CreateMilitaryStatus(ILookUpEntity LoanPaymentMethod)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the military status.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteMilitaryStatus(System.Linq.Expressions.Expression<Func<ILookUpEntity, bool>> Where)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region  IssuePlaces


        /// <summary>
        /// Gets the issue place by ID.
        /// </summary>
        /// <param name="IssuePlaceID">The issue place ID.</param>
        /// <returns></returns>
        public IIssuePlace GetIssuePlaceByID(Guid IssuePlaceID)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Finds the issue places.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<IIssuePlace> FindIssuePlaces(System.Linq.Expressions.Expression<Func<IIssuePlace, bool>> Where)
        {
            return _Database.IssuePlaces.Where(Where);

        }

        /// <summary>
        /// Creates the issue places.
        /// </summary>
        /// <param name="IssuePlace">The issue place.</param>
        public void CreateIssuePlaces(IIssuePlace IssuePlace)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the issue places.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteIssuePlaces(System.Linq.Expressions.Expression<Func<IIssuePlace, bool>> Where)
        {
            throw new NotImplementedException();
        }

        #region CustomerLoanHistory
        /// <summary>
        /// Gets the customer loan history by ID.
        /// </summary>
        /// <param name="CustomerLoanHistoryID">The customer loan history ID.</param>
        /// <returns></returns>
        public ICustomerLoanHistory GetCustomerLoanHistoryByID(Guid CustomerLoanHistoryID)
        {
            return this._Database.CustomerLoanHistories.Single(c => c.LoanHistoryID == CustomerLoanHistoryID);
        }

        /// <summary>
        /// Finds the customer histories.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ICustomerLoanHistory> FindCustomerHistories(System.Linq.Expressions.Expression<Func<ICustomerLoanHistory, bool>> Where)
        {
            return _Database.CustomerLoanHistories.Where(Where);
        }

        /// <summary>
        /// Creates the customer loan history.
        /// </summary>
        /// <param name="CustomerLoanHistory">The customer loan history.</param>
        public void CreateCustomerLoanHistory(ICustomerLoanHistory CustomerLoanHistory)
        {
            _Database.Insert<ICustomerLoanHistory>(CustomerLoanHistory);
        }

        /// <summary>
        /// Deletes the customer loan histories.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteCustomerLoanHistories(System.Linq.Expressions.Expression<Func<ICustomerLoanHistory, bool>> Where)
        {
            throw new NotImplementedException();
        }
        #endregion

        #endregion

        #region Governates


        /// <summary>
        /// Gets the governate by ID.
        /// </summary>
        /// <param name="GovernateID">The governate ID.</param>
        /// <returns></returns>
        public IGovernorate GetGovernateByID(Guid GovernateID)
        {
            return this._Database.Governorates.Single(g => g.GovernorateID == GovernateID);
        }

        /// <summary>
        /// Finds the governates.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<IGovernorate> FindGovernates(System.Linq.Expressions.Expression<Func<IGovernorate, bool>> Where)
        {
            return this._Database.Governorates.Where(Where);
        }

        /// <summary>
        /// Creates the governate.
        /// </summary>
        /// <param name="Governate">The governate.</param>
        public void CreateGovernate(IGovernorate Governate)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the governates.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteGovernates(System.Linq.Expressions.Expression<Func<IGovernorate, bool>> Where)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region BoardLoans

        /// <summary>
        /// Finds the board loans.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<IBoardLoan> FindBoardLoans(System.Linq.Expressions.Expression<Func<IBoardLoan, bool>> Where)
        {
            return this._Database.BoardLoans.Where(Where).OrderBy(bl => bl.Board.BoardDate);
        }

        /// <summary>
        /// Creates the board loan.
        /// </summary>
        /// <param name="BoardLoan">The board loan.</param>
        public void CreateBoardLoan(IBoardLoan BoardLoan)
        {
            this._Database.Insert<IBoardLoan>(BoardLoan);
        }

        /// <summary>
        /// Deletes the board loans.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteBoardLoans(System.Linq.Expressions.Expression<Func<IBoardLoan, bool>> Where)
        {
            this._Database.DeleteAll<IBoardLoan>(_Database.BoardLoans.Where(Where));
        }

        #endregion

        #region Board

        /// <summary>
        /// Gets the board by ID.
        /// </summary>
        /// <param name="BoardID">The board ID.</param>
        /// <returns></returns>
        public IBoard GetBoardByID(Guid BoardID)
        {
            return _Database.Boards.Single(b => b.BoardID == BoardID);
        }

        /// <summary>
        /// Finds the board.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<IBoard> FindBoard(System.Linq.Expressions.Expression<Func<IBoard, bool>> Where)
        {
            return _Database.Boards.Where(Where);
        }

        /// <summary>
        /// Creates the board.
        /// </summary>
        /// <param name="Board">The board.</param>
        public void CreateBoard(IBoard Board)
        {
            this._Database.Insert<IBoard>(Board);
        }

        /// <summary>
        /// Deletes the boards.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteBoards(System.Linq.Expressions.Expression<Func<IBoard, bool>> Where)
        {
            this._Database.DeleteAll<IBoard>(_Database.Boards.Where(Where));
        }

        /// <summary>
        /// Gets the last board number.
        /// </summary>
        /// <returns></returns>
        public int GetLastBoardNumber()
        {
            IQueryable<IBoard> loans = _Database.Boards;
            if (loans.Count() > 0)
            {
                int max = loans.Max(s => s.BoardNumber);
                return max;
            }
            else return 0;
        }

        #endregion

        #region BoardAttendees

        /// <summary>
        /// Gets the board attendees by ID.
        /// </summary>
        /// <param name="BoardAttendeeID">The board attendee ID.</param>
        /// <returns></returns>
        public IBoardAttendee GetBoardAttendeesByID(Guid BoardAttendeeID)
        {
            return this._Database.BoardAttendees.Single(a => a.BoardAttendeesID == BoardAttendeeID);
        }

        /// <summary>
        /// Finds the board attendees.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<IBoardAttendee> FindBoardAttendees(System.Linq.Expressions.Expression<Func<IBoardAttendee, bool>> Where)
        {
            return this._Database.BoardAttendees.Where(Where);
        }

        /// <summary>
        /// Creates the board attendee.
        /// </summary>
        /// <param name="Board">The board.</param>
        public void CreateBoardAttendee(IBoardAttendee Board)
        {
            this._Database.Insert<IBoardAttendee>(Board);
        }

        /// <summary>
        /// Deletes the board attendees.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteBoardAttendees(System.Linq.Expressions.Expression<Func<IBoardAttendee, bool>> Where)
        {
            this._Database.DeleteAll<IBoardAttendee>(this._Database.BoardAttendees.Where(Where));
        }

        #endregion

        #region LoanFeasibilityStudies


        /// <summary>
        /// Gets the loan feasibility study by ID.
        /// </summary>
        /// <param name="LoanFeasibilityStudyID">The loan feasibility study ID.</param>
        /// <returns></returns>
        public ILoanFeasibilityStudy GetLoanFeasibilityStudyByID(Guid LoanFeasibilityStudyID)
        {
            return _Database.LoanFeasibilityStudies.Single(s => s.LoanFeasabilityStudyID == LoanFeasibilityStudyID);
        }

        /// <summary>
        /// Finds the loan feasibility studies.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanFeasibilityStudy> FindLoanFeasibilityStudies(System.Linq.Expressions.Expression<Func<ILoanFeasibilityStudy, bool>> Where)
        {
            return _Database.LoanFeasibilityStudies.Where(Where);
        }

        /// <summary>
        /// Creates the loan feasibility study.
        /// </summary>
        /// <param name="study">The study.</param>
        public void CreateLoanFeasibilityStudy(ILoanFeasibilityStudy study)
        {
            this._Database.Insert<ILoanFeasibilityStudy>(study);
        }

        /// <summary>
        /// Deletes the loan feasibility studies.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteLoanFeasibilityStudies(System.Linq.Expressions.Expression<Func<ILoanFeasibilityStudy, bool>> Where)
        {
            this._Database.DeleteAll<ILoanFeasibilityStudy>(_Database.LoanFeasibilityStudies.Where(Where));
        }

        #endregion

        #region FeasibilityStudyAttachments


        /// <summary>
        /// Gets the loan feasibility study attachment by ID.
        /// </summary>
        /// <param name="LoanFeasibilityStudyAttachmentID">The loan feasibility study attachment ID.</param>
        /// <returns></returns>
        public ILoanFeasibilityStudyAttachment GetLoanFeasibilityStudyAttachmentByID(Guid LoanFeasibilityStudyAttachmentID)
        {
            return this._Database.LoanFeasibilityStudyAttachments.Single(a => a.LoanFeasabilityStudyAttachmentID == LoanFeasibilityStudyAttachmentID);
        }

        /// <summary>
        /// Finds the loan feasibility study attachments.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanFeasibilityStudyAttachment> FindLoanFeasibilityStudyAttachments(System.Linq.Expressions.Expression<Func<ILoanFeasibilityStudyAttachment, bool>> Where)
        {
            return this._Database.LoanFeasibilityStudyAttachments.Where(Where);
        }

        /// <summary>
        /// Creates the loan feasibility study attachment.
        /// </summary>
        /// <param name="StudyAttachment">The study attachment.</param>
        public void CreateLoanFeasibilityStudyAttachment(ILoanFeasibilityStudyAttachment StudyAttachment)
        {
            this._Database.Insert<ILoanFeasibilityStudyAttachment>(StudyAttachment);
        }

        /// <summary>
        /// Deletes the loan feasibility study attachment.
        /// </summary>
        /// <param name="StudyAttachment">The study attachment.</param>
        public void DeleteLoanFeasibilityStudyAttachment(ILoanFeasibilityStudyAttachment StudyAttachment)
        {
            this._Database.Delete<ILoanFeasibilityStudyAttachment>(StudyAttachment);
            this._Database.Delete<IImage>(StudyAttachment.AttachmentImage);
        }

        #endregion

        #region LoanSocialResearches


        /// <summary>
        /// Gets the loan social research by ID.
        /// </summary>
        /// <param name="LoanSocialResearchID">The loan social research ID.</param>
        /// <returns></returns>
        public ILoanSocialResearch GetLoanSocialResearchByID(Guid LoanSocialResearchID)
        {
            return _Database.LoanSocialResearches.Single(s => s.LoanSocialResearchID == LoanSocialResearchID);
        }

        /// <summary>
        /// Finds the loan social researches.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanSocialResearch> FindLoanSocialResearches(System.Linq.Expressions.Expression<Func<ILoanSocialResearch, bool>> Where)
        {
            return _Database.LoanSocialResearches.Where(Where);
        }

        /// <summary>
        /// Creates the loan social research.
        /// </summary>
        /// <param name="study">The study.</param>
        public void CreateLoanSocialResearch(ILoanSocialResearch study)
        {
            this._Database.Insert<ILoanSocialResearch>(study);
        }

        /// <summary>
        /// Deletes the loan social researches.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteLoanSocialResearches(System.Linq.Expressions.Expression<Func<ILoanSocialResearch, bool>> Where)
        {
            this._Database.DeleteAll<ILoanSocialResearch>(_Database.LoanSocialResearches.Where(Where));
        }

        #endregion

        #region SocialResearchAttachments


        /// <summary>
        /// Gets the loan social research attachment by ID.
        /// </summary>
        /// <param name="LoanSocialResearchAttachmentID">The loan social research attachment ID.</param>
        /// <returns></returns>
        public ILoanSocialResearchAttachment GetLoanSocialResearchAttachmentByID(Guid LoanSocialResearchAttachmentID)
        {
            return this._Database.LoanSocialResearchAttachments.Single(a => a.LoanSocialResearchAttachmentID == LoanSocialResearchAttachmentID);
        }

        /// <summary>
        /// Finds the loan social research attachments.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanSocialResearchAttachment> FindLoanSocialResearchAttachments(System.Linq.Expressions.Expression<Func<ILoanSocialResearchAttachment, bool>> Where)
        {
            return this._Database.LoanSocialResearchAttachments.Where(Where);
        }

        /// <summary>
        /// Creates the loan social research attachment.
        /// </summary>
        /// <param name="researchAttachment">The research attachment.</param>
        public void CreateLoanSocialResearchAttachment(ILoanSocialResearchAttachment researchAttachment)
        {
            this._Database.Insert<ILoanSocialResearchAttachment>(researchAttachment);
        }

        /// <summary>
        /// Deletes the loan social research attachment.
        /// </summary>
        /// <param name="researchAttachment">The research attachment.</param>
        public void DeleteLoanSocialResearchAttachment(ILoanSocialResearchAttachment researchAttachment)
        {
            this._Database.Delete<ILoanSocialResearchAttachment>(researchAttachment);
            this._Database.Delete<IImage>(researchAttachment.AttachmentImage);
        }

        #endregion

        #region BoardAttachements

        /// <summary>
        /// Gets the board attachment.
        /// </summary>
        /// <param name="BoardAttachmentID">The board attachment ID.</param>
        /// <returns></returns>
        public IBoardAttachment GetBoardAttachment(Guid BoardAttachmentID)
        {
            return _Database.BoardAttachments.Single(bA => bA.BoardAttachmentID == BoardAttachmentID);
        }

        /// <summary>
        /// Finds the board attachments.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<IBoardAttachment> FindBoardAttachments(System.Linq.Expressions.Expression<Func<IBoardAttachment, bool>> Where)
        {
            return _Database.BoardAttachments.Where(Where);
        }

        /// <summary>
        /// Creates the board attachment.
        /// </summary>
        /// <param name="BoardAttachment">The board attachment.</param>
        public void CreateBoardAttachment(IBoardAttachment BoardAttachment)
        {
            this._Database.Insert<IBoardAttachment>(BoardAttachment);
        }

        /// <summary>
        /// Deletes the board attachment.
        /// </summary>
        /// <param name="BoardAttachment">The board attachment.</param>
        public void DeleteBoardAttachment(IBoardAttachment BoardAttachment)
        {
            _Database.Delete<IBoardAttachment>(BoardAttachment);
            _Database.Delete<IImage>(BoardAttachment.AttachmentImage);
        }

        #endregion

        #region Images

        /// <summary>
        /// Finds the images.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<IImage> FindImages(System.Linq.Expressions.Expression<Func<IImage, bool>> where)
        {
            return this._Database.Images.Where(where);
        }

        /// <summary>
        /// Creates the image.
        /// </summary>
        /// <param name="image">The image.</param>
        public void CreateImage(IImage image)
        {
            this._Database.Insert<IImage>(image);
        }

        #endregion

        #region Receipts

        /// <summary>
        /// Finds the receipts.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<IReceipt> FindReceipts(System.Linq.Expressions.Expression<Func<IReceipt, bool>> where)
        {
            return this._Database.Receipts.Where(where);
        }

        #endregion

        #region LoanExecution

        /// <summary>
        /// Gets the loan execution.
        /// </summary>
        /// <param name="loanExecutionID">The loan execution ID.</param>
        /// <returns></returns>
        public ILoanExecution GetLoanExecution(Guid loanExecutionID)
        {
            return this._Database.LoanExecutions.Single(loanExcution => loanExcution.LoanExecutionID == loanExecutionID);
        }

        /// <summary>
        /// Finds the loan executions.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanExecution> FindLoanExecutions(System.Linq.Expressions.Expression<Func<ILoanExecution, bool>> where)
        {
            return _Database.LoanExecutions.Where(where);
        }

        /// <summary>
        /// Creates the loan execution.
        /// </summary>
        /// <param name="loanExecution">The loan execution.</param>
        public void CreateLoanExecution(ILoanExecution loanExecution)
        {
            _Database.Insert<ILoanExecution>(loanExecution);
        }

        /// <summary>
        /// Deletes the loan execution.
        /// </summary>
        /// <param name="loanExecution">The loan execution.</param>
        public void DeleteLoanExecution(ILoanExecution loanExecution)
        {
            _Database.Delete<ILoanExecution>(loanExecution);
        }

        /// <summary>
        /// Finds the loan execution attachments.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanExecutionAttachment> FindLoanExecutionAttachments(System.Linq.Expressions.Expression<Func<ILoanExecutionAttachment, bool>> where)
        {
            return _Database.LoanExecutionsAttachmens.Where(where);
        }

        /// <summary>
        /// Creates the loan execution attachment.
        /// </summary>
        /// <param name="attachment">The attachment.</param>
        public void CreateLoanExecutionAttachment(ILoanExecutionAttachment attachment)
        {
            _Database.Insert<ILoanExecutionAttachment>(attachment);
        }

        /// <summary>
        /// Deletes the loan executon attachment.
        /// </summary>
        /// <param name="attachment">The attachment.</param>
        public void DeleteLoanExecutonAttachment(ILoanExecutionAttachment attachment)
        {
            _Database.Delete<ILoanExecutionAttachment>(attachment);
            _Database.Delete<IImage>(attachment.AttachmentImage);
        }

        #endregion

        #region LoanExecutionInstallment

        /// <summary>
        /// Gets the loan execution installment.
        /// </summary>
        /// <param name="loanExecutionInstallmentId">The loan execution installment ID.</param>
        /// <returns></returns>
        public ILoanExecutionInstallment GetLoanExecutionInstallment(Guid loanExecutionInstallmentId)
        {
            return this._Database.LoanExecutionInstallments.Single(installment => installment.LoanExecutionInstallmentID == loanExecutionInstallmentId);
        }

        /// <summary>
        /// Finds the loan execution installments.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanExecutionInstallment> FindLoanExecutionInstallments(System.Linq.Expressions.Expression<Func<ILoanExecutionInstallment, bool>> where)
        {
            return this._Database.LoanExecutionInstallments.Where(where);
        }

        /// <summary>
        /// Creates the loan execution installment.
        /// </summary>
        /// <param name="loanExecutionInstallment">The loan execution installment.</param>
        public void CreateLoanExecutionInstallment(ILoanExecutionInstallment loanExecutionInstallment)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the loan execution installment.
        /// </summary>
        /// <param name="loanExecutionInstallment">The loan execution installment.</param>
        public void DeleteLoanExecutionInstallment(ILoanExecutionInstallment loanExecutionInstallment)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region LoanInstallmentPayment

        /// <summary>
        /// Gets the loan installment payment.
        /// </summary>
        /// <param name="loanInstallmentPaymentID">The loan installment payment ID.</param>
        /// <returns></returns>
        public ILoanInstallmentPayment GetLoanInstallmentPayment(Guid loanInstallmentPaymentID)
        {
            return _Database.LoanInstallmentPayments.Single(payment => payment.LoanInstallmentPaymentID.Equals(loanInstallmentPaymentID));
        }

        /// <summary>
        /// Finds the loan installment payments.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanInstallmentPayment> FindLoanInstallmentPayments(System.Linq.Expressions.Expression<Func<ILoanInstallmentPayment, bool>> where)
        {
            return _Database.LoanInstallmentPayments.Where(where);
        }

        /// <summary>
        /// Creates the loan installment payment.
        /// </summary>
        /// <param name="LoanInstallmentPayment">The loan installment payment.</param>
        public void CreateLoanInstallmentPayment(ILoanInstallmentPayment LoanInstallmentPayment)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the loan installment payment.
        /// </summary>
        /// <param name="LoanInstallmentPayment">The loan installment payment.</param>
        public void DeleteLoanInstallmentPayment(ILoanInstallmentPayment LoanInstallmentPayment)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region LoanIntstallmentWarning

        /// <summary>
        /// Gets the loan installment warning.
        /// </summary>
        /// <param name="LoanInstallmentWarningID">The loan installment warning ID.</param>
        /// <returns></returns>
        public ILoanInstallmentWarning GetLoanInstallmentWarning(Guid LoanInstallmentWarningID)
        {
            return _Database.LoanInstallmentWarnings.Single(warning => warning.LoanIntstallmentWarningID.Equals(LoanInstallmentWarningID));
        }

        /// <summary>
        /// Finds the loan installment warnings.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanInstallmentWarning> FindLoanInstallmentWarnings(System.Linq.Expressions.Expression<Func<ILoanInstallmentWarning, bool>> where)
        {
            return _Database.LoanInstallmentWarnings.Where(where);
        }

        /// <summary>
        /// Creates the loan installment warning.
        /// </summary>
        /// <param name="loanIntstallmentWarning">The loan intstallment warning.</param>
        public void CreateLoanInstallmentWarning(ILoanInstallmentWarning loanIntstallmentWarning)
        {
            _Database.Insert<ILoanInstallmentWarning>(loanIntstallmentWarning);
        }

        /// <summary>
        /// Deletes the loan installment warning.
        /// </summary>
        /// <param name="where">The where.</param>
        public void DeleteLoanInstallmentWarning(System.Linq.Expressions.Expression<Func<ILoanInstallmentWarning, bool>> where)
        {
            _Database.DeleteAll(_Database.LoanInstallmentWarnings.Where(where));
        }

        #endregion

        #region LoanInstallmentWarningAttachment

        /// <summary>
        /// Gets the loan installment warning attachment.
        /// </summary>
        /// <param name="LoanInstallmentWarningAttachmentID">The loan installment warning attachment ID.</param>
        /// <returns></returns>
        public ILoanInstallmentWarningAttachment GetLoanInstallmentWarningAttachment(Guid LoanInstallmentWarningAttachmentID)
        {
            return _Database.LoanInstallmentWarningAttachments.Single(warningAttach => warningAttach.LoanInstallmentWarningAttachmentID.Equals(LoanInstallmentWarningAttachmentID));
        }

        /// <summary>
        /// Finds the loan installment warning attachment.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanInstallmentWarningAttachment> FindLoanInstallmentWarningAttachment(System.Linq.Expressions.Expression<Func<ILoanInstallmentWarningAttachment, bool>> where)
        {
            return _Database.LoanInstallmentWarningAttachments.Where(where);
        }

        /// <summary>
        /// Creates the loan installment warning attachment.
        /// </summary>
        /// <param name="loanInstallmentWarningAttachment">The loan installment warning attachment.</param>
        public void CreateLoanInstallmentWarningAttachment(ILoanInstallmentWarningAttachment loanInstallmentWarningAttachment)
        {
            _Database.Insert<ILoanInstallmentWarningAttachment>(loanInstallmentWarningAttachment);
        }

        /// <summary>
        /// Deletes the loan installment warning attachment.
        /// </summary>
        /// <param name="where">The where.</param>
        public void DeleteLoanInstallmentWarningAttachment(System.Linq.Expressions.Expression<Func<ILoanInstallmentWarningAttachment, bool>> where)
        {
            _Database.DeleteAll(_Database.LoanInstallmentWarningAttachments.Where(where));
        }

        #endregion


        #region LoanCases

        /// <summary>
        /// Gets the loan case by ID.
        /// </summary>
        /// <param name="loanCaseID">The loan case ID.</param>
        /// <returns></returns>
        public ILoanCase GetLoanCaseByID(Guid loanCaseID)
        {
            return _Database.LoanCases.Single(loanCase => loanCase.LoanCaseID == loanCaseID);
        }

        /// <summary>
        /// Finds the loan cases.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanCase> FindLoanCases(System.Linq.Expressions.Expression<Func<ILoanCase, bool>> where)
        {
            return _Database.LoanCases.Where(where);
        }

        /// <summary>
        /// Creates the loan case.
        /// </summary>
        /// <param name="loanCase">The loan case.</param>
        public void CreateLoanCase(ILoanCase loanCase)
        {
            _Database.Insert<ILoanCase>(loanCase);
        }

        /// <summary>
        /// Deletes the loan cases.
        /// </summary>
        /// <param name="where">The where.</param>
        public void DeleteLoanCases(System.Linq.Expressions.Expression<Func<ILoanCase, bool>> where)
        {
            _Database.DeleteAll(_Database.LoanCases.Where(where));
        }

        #endregion

        #region  LoanCancellingDebt


        /// <summary>
        /// Gets the loan cancelling debt.
        /// </summary>
        /// <param name="loanCancellingDebtID">The loan cancelling debt ID.</param>
        /// <returns></returns>
        public ILoanCancellingDebt GetLoanCancellingDebt(Guid loanCancellingDebtID)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Finds the loan cancelling debts.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanCancellingDebt> FindLoanCancellingDebts(System.Linq.Expressions.Expression<Func<ILoanCancellingDebt, bool>> where)
        {
            return _Database.LoanCancellingDebts.Where(where);
        }

        /// <summary>
        /// Creates the loan cancelling debt.
        /// </summary>
        /// <param name="loanCancellingDebt">The loan cancelling debt.</param>
        public void CreateLoanCancellingDebt(ILoanCancellingDebt loanCancellingDebt)
        {
            _Database.Insert<ILoanCancellingDebt>(loanCancellingDebt);
        }

        /// <summary>
        /// Deletes the loan cancelling debts.
        /// </summary>
        /// <param name="where">The where.</param>
        public void DeleteLoanCancellingDebts(System.Linq.Expressions.Expression<Func<ILoanCancellingDebt, bool>> where)
        {
            _Database.DeleteAll(_Database.LoanCancellingDebts.Where(where));
        }

        #endregion

        #region LoanCancellingDebtAttachment


        /// <summary>
        /// Gets the loan cancelling debt attachment.
        /// </summary>
        /// <param name="loanCancellingDebtAttachmentID">The loan cancelling debt attachment ID.</param>
        /// <returns></returns>
        public ILoanCancellingDebtAttachment GetLoanCancellingDebtAttachment(Guid loanCancellingDebtAttachmentID)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Finds the loan cancelling debt attachments.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<ILoanCancellingDebtAttachment> FindLoanCancellingDebtAttachments(System.Linq.Expressions.Expression<Func<ILoanCancellingDebtAttachment, bool>> where)
        {
            return _Database.LoanCancellingDebtAttachments.Where(where);
        }

        /// <summary>
        /// Creates the loan cancelling debt attachment.
        /// </summary>
        /// <param name="loanCancellingDebtAttachment">The loan cancelling debt attachment.</param>
        public void CreateLoanCancellingDebtAttachment(ILoanCancellingDebtAttachment loanCancellingDebtAttachment)
        {
            _Database.Insert<ILoanCancellingDebtAttachment>(loanCancellingDebtAttachment);
        }

        /// <summary>
        /// Deletes the loan cancelling debt attachments.
        /// </summary>
        /// <param name="where">The where.</param>
        public void DeleteLoanCancellingDebtAttachments(System.Linq.Expressions.Expression<Func<ILoanCancellingDebtAttachment, bool>> where)
        {
            _Database.DeleteAll(_Database.LoanCancellingDebtAttachments.Where(where));
        }

        #endregion
    }
}
